package com.sc.contact.create;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Reader;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVRecord;

public class Create {

	public Create(String orgName, String user, String password, String userCsvPath, String datPath) throws IOException {
		DatFormat df = new DatFormat(user, password);

		Reader in = new FileReader(userCsvPath);
		Iterable<CSVRecord> records = CSVFormat.DEFAULT.parse(in);
		int i = 0;
		for (CSVRecord record : records) {
			if (i>1) {
				String state = record.get(5);
				if ("ACTIVE".equals(state)) {
					String email = record.get(2);
					String dept = record.get(4);
					if (dept.length()==0) {
						dept = "Default";
					}
					System.out.println("Processing "+email+" "+dept);
					df.addEntry(dept, email);
				}
			}
			i++;
		}
		df.generate(new PrintWriter(new FileWriter(datPath)), orgName.replaceAll(" ", ";"));
		System.out.println("You can now import the file "+datPath+" into your Sametime clients.");
	}

	public static final void main(String[] args) throws IOException {
		if (args.length!=5) {
			System.out.println("");
			System.out.println("Connections Cloud Sametime Utility");
			System.out.println("");
			System.out.println("Provided as is, use at your own risk.");
			System.out.println("");
			System.out.println("Usage:");
			System.out.println("java -jar STContactCreator-0.0.1-SNAPSHOT-shaded <Organisation-Name> <username> <password> <userCsvFile> <outputfile>");
			System.out.println("");
			System.out.println("");
			System.out.println("");
		}
		else {
			new Create(args[0],args[1],args[2], args[3], args[4]);
		}
	}
}
